package com.jinyou.utils.common;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: @Created by yangtk
 * @Date: @Date 2020/12/3 14:43
 * @Email: yangtongkuan@qq.com
 * @To change this template use File | Settings | File Templates.
 * @desc
 */
public class JYEasyExcelUtils {

    /**
     * 导出excel
     *
     * @param response
     * @param exportFileName
     * @param title
     * @param sheetName
     * @param headList
     * @param rowDataList
     * @throws IOException
     */
    public static void exportExcel(HttpServletResponse response, String exportFileName, String title, String sheetName, List<ExcelExportEntity> headList, List<Map<String, Object>> rowDataList) throws IOException {
        response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(exportFileName + ".xls", "UTF-8"));
        ServletOutputStream stream = response.getOutputStream();
        exportExcel(stream, title, sheetName, headList, rowDataList);
        stream.close();
    }

    /**
     * 导出excel
     *
     * @param outputStream
     * @param title
     * @param sheetName
     * @param headList
     * @param rowDataList
     * @throws IOException
     */
    public static void exportExcel(OutputStream outputStream, String title, String sheetName, List<ExcelExportEntity> headList, List<Map<String, Object>> rowDataList) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, sheetName), headList, rowDataList);
        workbook.write(outputStream);
        workbook.close();
    }

    /**
     * 参考 o2o.service.order.other.OtherOrderService.readExcelFileV2()
     * 导入excel
     *
     * @param inputStream 文件流
     * @param titleRows   表格标题行数
     * @param headRows    表头行数
     * @param startRows
     * @param clazz
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<T> importExcel(InputStream inputStream, int titleRows, int headRows, int startRows, Class<?> clazz) throws Exception {
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows); // 设置标题行数
        params.setHeadRows(headRows); // 表头行数
        params.setStartRows(startRows); // 字段真正值和列标题之间的距离
        return importExcel(inputStream, clazz, params);
    }

    /**
     * 导入excel
     *
     * @param inputStream 文件流
     * @param clazz       对象
     * @param params      导入的表头设置
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<T> importExcel(InputStream inputStream, Class<?> clazz, ImportParams params) throws Exception {
        return ExcelImportUtil.importExcel(inputStream, clazz, params);
    }


}
